-- Keybinds for Neovim

local keys = vim.api

local leader = ' '
local opts = {
    noremap = true,
    silent = true
}

-- Custom commands
local term = ':wincmd s <BAR> :wincmd j <BAR> :resize 12 <BAR> :term <CR>'

-- Cheatsheet:
-- vim.api.nvim_set_keymap(mode, key(s), command(s), options)
-- modes:
-- n: normal
-- i: insert
-- v: visual
-- Keys:
-- <A>: Alt
-- <C>: Ctrl
-- <ESC>: Escape

-- Leader
vim.g.mapleader = leader
vim.b.mapleader = leader

-- General
vim.api.nvim_set_keymap('t', '<ESC>', '<C-\\><C-n>', opts) -- Change modes in terminal
vim.api.nvim_set_keymap('n', '<A-t>', term, opts) -- Open a terminal
vim.api.nvim_set_keymap('n', 'w', ':w <CR>', opts) -- Save buffer
vim.api.nvim_set_keymap('n', 's', ':w <CR>', opts) -- Save buffer
vim.api.nvim_set_keymap('n', '<C-s>', ':w <CR>', opts) -- Save buffer

-- Window controls
vim.api.nvim_set_keymap('n', '<leader>h', ':wincmd h <CR>', opts)
vim.api.nvim_set_keymap('n', '<leader>j', ':wincmd j <CR>', opts)
vim.api.nvim_set_keymap('n', '<leader>k', ':wincmd k <CR>', opts)
vim.api.nvim_set_keymap('n', '<leader>l', ':wincmd l <CR>', opts)
vim.api.nvim_set_keymap('n', '<A-v>', ':vsplit <CR>', opts)
vim.api.nvim_set_keymap('n', '<A-s>', ':split <CR>', opts)

-- Tab controls
vim.api.nvim_set_keymap('n', '<C-[>', ':tabprevious <CR>', opts)
vim.api.nvim_set_keymap('n', '<C-]>', ':tabn <CR>', opts)
vim.api.nvim_set_keymap('n', '<C-q>', ':tabclose <CR>', opts)
vim.api.nvim_set_keymap('n', '<C-t>', ':tabnew <BAR> :NERDTree <CR>', opts)

--[[ LSP controls
:lua vim.lsp.buf.definition()<CR>
:lua vim.lsp.buf.hover()<CR>
:lua vim.lsp.buf.implementation()<CR>
:lua vim.lsp.buf.signature_help()<CR>
:lua vim.lsp.buf.type_definition()<CR>
:lua vim.lsp.buf.references()<CR>
:lua vim.lsp.buf.document_symbol()<CR>
:lua vim.lsp.buf.workspace_symbol()<CR>
:lua vim.lsp.buf.declaration()<CR>
]]

vim.cmd([[
"-- Nerdtree
nmap <leader>f :NERDTreeToggle <CR>
nmap <C-f> :NERDTreeToggle <CR>

"-- Undotree
nmap <A-u> :UndotreeShow <CR>

"-- FZF
nmap <A-f> :Files <CR>
nmap <A-a> :Ag <CR>
nmap <A-r> :Rg <CR>
]])
